package com.example.bjsfpost.schdules;

import com.example.bjsfpost.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Scheduled除过cron还有三种方式：fixedRate，fixedDelay，initialDelay 只是说是 fixedRate 任务两次执行时间间隔是任务的开始点，
 * 而 fixedDelay 的间隔是前次任务的结束与下次任务的开始
 */
@Async
@Component
@Slf4j
public class TaskSchedule implements Serializable {

    @Scheduled(initialDelay = 2000L, fixedDelay = 10 * 60 * 1000L)
    public void scheduled1() {
        String url = "http://192.198.30.44:8083/rest/v1/bjsf/process/linkPartToHistoryProcessPlan?assignType=HISTORY_PLAN";
        log.info("HISTORY_PLAN url == {}", url);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        String s = HttpUtil.sendPost(url, headers, "{}");
        log.info("HISTORY_PLAN response == {}", s);
    }

    @Scheduled(initialDelay = 2000L, fixedDelay = 10 * 60 * 1000L)
    public void scheduled2() {
        String url = "http://192.198.30.44:8083/rest/v1/bjsf/process/linkPartToHistoryProcessPlan?assignType=DERIVATE_PLAN";
        log.info("DERIVATE_PLAN url == {}", url);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        String s = HttpUtil.sendPost(url, headers, "{}");
        log.info("DERIVATE_PLAN response == {}", s);
    }

    @Scheduled(initialDelay = 2000L, fixedDelay = 10 * 60 * 1000L)
    public void scheduled3() {
        String url = "http://192.198.30.44:8083/rest/v1/bjsf/process/autoStartProcessByProcessTask?assignType=PROCESS_TASK";
        log.info("PROCESS_TASK url == {}", url);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        String s = HttpUtil.sendPost(url, headers, "{}");
        log.info("PROCESS_TASK response == {}", s);
    }
}
